﻿using Microsoft.Hadoop.Client;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ViewMaker;
using ViewMaker.Core;
using ViewMaker.Core.Controls;


namespace HDInsightStudio
{
    [View(typeof(SqoopStudioView))]
    [ViewProperty(ViewControl.Properties.WindowHeight, 400)]
    [ViewProperty(ViewControl.Properties.WindowWidth, 600)]
    public class SqoopStudio : JobViewModelBase
    {

        public string InputCommand
        {
            get { return _inputCommand; }
            set { _inputCommand = value; OnPropertyChanged("InputCommand"); }
        }
        private string _inputCommand;

        public bool Busy
        {
            get { return _busy; }
            set { _busy = value; OnPropertyChanged(null); }
        }
        private bool _busy;

        public string StdErr
        {
            get { return _StdErr; }
            set { _StdErr = value; OnPropertyChanged("StdErr"); }
        }
        private string _StdErr;

        private string HomeFolder = "/SqoopReduceStudio";
        public SqoopStudio()
            : this(DateTime.Now.ToString("yyyyMMddHHmmss"))
        {
        }

        public SqoopStudio(string jobId)
            : base(jobId)
        {
            Status = HomeFolder + "/" + jobId + "/Status/";

        }


        public ICommand ExecuteMapReduceCommand
        {
            get { return this.CreateCommand(ExecuteMapReduce, (x) => !Busy); }
        }
        private void ExecuteMapReduce()
        {
            if (!string.IsNullOrWhiteSpace(InputCommand))
            {

                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += (s, e) =>
                {
                    Busy = true;

                    var sqoop = new SqoopJobCreateParameters()
                    {
                        Command = InputCommand,
                        StatusFolder = Status,
                        
                    };

                    var jobResult = Config.JobClient.CreateSqoopJobAsync(sqoop).WaitForResult();

                    WaitForJobCompletion(jobResult, ProgressProc);
                    StdErr = ReadError();

                    Busy = false;

                };
                worker.RunWorkerAsync();

            }
        }
        private bool ProgressProc(JobDetails job)
        {
            StdErr = ReadError();
            return true;
        }

        protected override void Save()
        {
            JObject data = new JObject{
                { "Type", this.GetType().Name },
                { "InputCommand", InputCommand },
            };
            SaveData(data);
        }

        protected override void Load()
        {
            var data = LoadData();
            if (data == null) return;
            if ((string)data.Property("Type").Value != this.GetType().Name)
            {
                this.ShowMessage("Invalid selected file. " + data.Property("Type").Value);
            }
            else
            {
                this.InputCommand = (string)data.Property("InputCommand").Value;
            }
        }

    }
}
